/**
 * 
 */
package xx.services.teach;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.sql.RowSet;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import xx.services.Passport;
import xx.services.util.Constants;
import xx.teach.entity.Answer;
import xx.teach.entity.Group;
import xx.teach.entity.Job;

/**
 * @author Administrator 2014-3-22
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/teach/answer")
public class AnswerRest extends Passport
{

    /**
     * @param request
     */
    public AnswerRest(@Context
    HttpServletRequest request)
    {
        super(request);
    }

    @POST
    @Path("/upclass")
    public String addAnswerOnclass(@FormParam("account_id")
    long accountId, @FormParam("content")
    String content, @FormParam("attach_path")
    String attachPath)
    {
        try
        {
            if (accountId == 0)
                return error("accountId is 0");

            Answer answer = new Answer(qr, owner);
            if (content != null && !content.isEmpty())
                answer.setContent(content);

            if (attachPath != null && !attachPath.isEmpty())
                answer.setAttachPath(attachPath);

            answer.setAccountId(accountId).setStatus(0);

            Job job = new Job(qr, owner);
            job = job.getJobOnclass();

            if (job == null)
                return error("获取作业发生错误");
            else
                answer.setJobId(job.getJobId());

            long answerId = answer.insert();
            return ok(answerId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 获取未批改的学生作业
     * 
     * @return
     * @throws SQLException
     */
    @GET
    @Path("/uncheck")
    public String getUncheckAnswer()
    {
        try
        {
            Answer answer = new Answer(qr, owner);
            RowSet rs = answer.getUncheckAnswer();
            return ok(rs);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    @PUT
    @Path("/update")
    public String updateGroup(@FormParam("answer_id")
    long answerId, @FormParam("status")
    int status)
    {
        try
        {
            Answer answer = new Answer(qr, owner);
            int count = answer.setAnswerId(answerId).setStatus(status).update(Answer.STATUS);
            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }
}
